package com.taritari.journal.services;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.taritari.journal.entity.po.WordCloudAnalyse;
import com.taritari.journal.enums.ResultCode;
import com.taritari.journal.utils.Result;
import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * @author taritari
 * @date 2024-01-12 17:19
 * @description 邮件发送的
 */
@Component
public class MyMailService {
    @Resource
    private JavaMailSender javaMailSender;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private TemplateEngine templateEngine;
    /**
     * 发送邮件
     * */
    public Result sendMail(String to,String opt){
        String template = "";
        if (opt.equals("register")){
            template = "verifyEmail";
        }else if (opt.equals("retrieve")){
            template = "retrieveEmail";
        }
        try {
            String verify = RandomUtil.randomNumbers(6);
            String onlySign = RandomUtil.randomString(10);
//            stringRedisTemplate.opsForValue().set(onlySign, verify);
//            //设置有效期30分钟
//            stringRedisTemplate.expire(onlySign, Duration.ofMillis(30));
            stringRedisTemplate.opsForValue().set(onlySign, verify,  30 * 60, TimeUnit.SECONDS);
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom("1150082783@qq.com");
            mimeMessageHelper.setTo(to);
            mimeMessageHelper.setSubject("你的日记-验证码");

            // HTML 模板
            Context context = new Context();
            // 设置模板
            context.setVariable("verificationCode", verify);
            String content = templateEngine.process(template, context);
            mimeMessageHelper.setText(content, true); // 关键是第二个参数，开启将富文本渲染为HTML

            javaMailSender.send(mimeMessage);//发送邮件
            return Result.success(onlySign);
        }catch (Exception e){
            return Result.fail(ResultCode.ERROR);
        }
    }
    /**
     * 发送邮件
     * */
    public Result sendWordCloudReport(WordCloudAnalyse wordCloudAnalyse,String email){
        String template = "wordCloud";
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom("1150082783@qq.com");
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("你的日记-心旅札记");
            // HTML 模板
            Context context = new Context();
            // 设置模板
            context.setVariable("theme", wordCloudAnalyse.getTheme());//标题
            context.setVariable("analyse", wordCloudAnalyse.getAnalyse());//分析
            context.setVariable("content", wordCloudAnalyse.getContent());//内容
            context.setVariable("suggest", wordCloudAnalyse.getSuggest());//建议
            context.setVariable("date", DateUtil.today());//日期
            String content = templateEngine.process(template, context);
            mimeMessageHelper.setText(content, true); // 关键是第二个参数，开启将富文本渲染为HTML

            javaMailSender.send(mimeMessage);//发送邮件
            return Result.success();
        }catch (Exception e){
            return Result.fail(ResultCode.ERROR);
        }
    }
}
